package com.newzqxq.koalamoney.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.newzqxq.koalamoney.R;
import com.newzqxq.koalamoney.entity.BannerBean;
import com.newzqxq.koalamoney.entity.HomeBean;
import com.newzqxq.koalamoney.widget.ColorTextView;
import com.zhouwei.mzbanner.MZBannerView;

import java.util.List;

/**
 * 定投adapter
 * Created by ZQXQ-Developer on 2017/7/19.
 */

public class HomeAdapter extends BaseRecyclerAdapter {
    private List<HomeBean> items;
    private Context mContext;
    public OnItemClickListener mItemClickListener;
    private LayoutInflater inflater;

    public HomeAdapter(List<HomeBean> items, Context mContext) {
        this.items = items;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(viewType, parent, false);
        return new mViewHolder(view, mItemClickListener);
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.home_item;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((mViewHolder) holder).bind(items.get(position));
    }

    @Override
    public int getItemCount() {
        return items == null ? 0 : items.size();
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view);
    }

    public HomeAdapter setmItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
        return this;
    }

    private class mViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView imTips;
        private ImageView imEnterTips;
        private TextView tvRate;//利率
        private TextView tvHomeDay;//天数
        private TextView tvHomeMoney;
        private OnItemClickListener clickListener;
        private Button layout;

        public mViewHolder(View view, OnItemClickListener mItemClickListener) {
            super(view);
            imTips = (ImageView) view.findViewById(R.id.imHomeTips);
            imEnterTips = (ImageView) view.findViewById(R.id.imEnterTips);
            tvRate = (TextView) view.findViewById(R.id.tvHomeRate);
            tvHomeDay = (TextView) view.findViewById(R.id.tvHomeDay);
            tvHomeMoney = (TextView) view.findViewById(R.id.tvHomeMoney);
            this.clickListener = mItemClickListener;
            layout = (Button) view.findViewById(R.id.btnInvest);
            layout.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (clickListener != null) {
                clickListener.onItemClickListener(view);
            }
        }

        public void bind(HomeBean plan) {
            tvRate.setText(plan.getRate());
            String pattern = "定期{" + plan.getDay() + "日}";
            CharSequence chars = ColorTextView.from(pattern).withSeparator("{}")
                    .innerColor(0xFFE6454A).outerColor(0xFF666666).format();
            tvHomeDay.setText(chars);


            String patterns = "起投{" + plan.getMoney() + "元}";
            CharSequence char1 = ColorTextView.from(patterns).withSeparator("{}")
                    .innerColor(0xFFE6454A).outerColor(0xFF666666).format();
            tvHomeMoney.setText(char1);
            if (plan.getState() > 2) {
                imTips.setImageResource(R.mipmap.img_home_licai);
                imEnterTips.setVisibility(View.GONE);
            } else {
                imTips.setImageResource(R.mipmap.img_home_newman);
                imEnterTips.setVisibility(View.VISIBLE);
            }
        }
    }

    private class BannerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private MZBannerView banner;
        private OnItemClickListener clickListener;

        public BannerViewHolder(View view, OnItemClickListener mItemClickListener) {
            super(view);
            banner = (MZBannerView) view.findViewById(R.id.home_banner);
            this.clickListener = mItemClickListener;
            itemView.setOnClickListener(this);
        }

        public void bind(final List<BannerBean> bannerBean) {


        }

        @Override
        public void onClick(View view) {
            if (clickListener != null) {
                clickListener.onItemClickListener(view);
            }
        }
    }
}
